<template>
  <el-dialog title="订单确认" :visible.sync="open" width="600px" append-to-body>
    <el-form ref="form" :model="form" label-width="120px" :disabled="true">
      <el-form-item label="订单号" prop="orderNumber">
        {{form.orderNumber}}
      </el-form-item>
      <el-form-item label="序列号" prop="treasureSerialNumber">
        {{ form.treasureSerialNumber }}
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        {{ form.startTime }}
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        {{ form.endTime }}
      </el-form-item>
      <el-form-item label="使用时长(分钟)" prop="duration">
        {{ form.duration }}
      </el-form-item>
      <el-form-item label="费用(元)" prop="account">
        {{ form.account }}
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { endOrder } from "@/api/system/order";
export default {
  data() {
    return {
      // 遮罩层
      open: false,
      // 表单参数
      form: {},
    }
  },
  methods: {
    show(row) {
      this.form = row;
      this.form.endTime = this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
      this.form.duration = this.$moment(this.form.endTime).diff(this.form.startTime, 'minutes')
      this.calculateAccount();
      this.open = true;
    },
    calculateAccount() {
      const durationInMinutes = this.form.duration;
      const durationInHours = Math.ceil(durationInMinutes / 60); // 不足60分钟按1小时算
      let account = 0;

      if (durationInHours <= 24) {
        account = Math.min(durationInHours * 10, 40); // 10元/小时，但24小时内封顶40元
      } else {
        const fullDays = Math.floor(durationInHours / 24);
        const remainingHours = durationInHours % 24;
        account = fullDays * 40 + Math.min(remainingHours * 10, 40); // 每24小时40元，剩余小时按10元/小时算
      }

      this.form.account = Math.min(account, 99); // 封顶99元
    },
    submitForm() {
      endOrder(this.form).then(() => {
        this.$modal.msgSuccess("已结束订单");
        this.open = false;
        this.$emit('ok');
      })
    },
    cancel() {
      this.open = false;
    }
  }
}
</script>
<style scoped lang="scss">

</style>
